package com.sky.controller.backstage;

import com.sky.dto.BackstageArticleAddDTO;
import com.sky.dto.BackstageArticleQueryDTO;
import com.sky.dto.BackstageArticleUpdateDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.ArticleService;
import com.sky.service.BackstageArticleService;
import com.sky.service.BackstageCommentService;
import com.sky.service.BackstageTagService;
import com.sky.vo.ArticleInfoVO;
import com.sky.vo.BackstageArticleSearchVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 贾晓晨
 * @version 1.0
 */
@RestController
@RequestMapping("/backstage/article")
@Slf4j
@Api(tags = "后台文章管理接口")
public class BackstageArticleController {
    @Autowired
    private BackstageArticleService backstageArticleService;
    @Autowired
    private BackstageCommentService backstageCommentService;
    @Autowired
    private ArticleService articleService;

    @GetMapping
    @ApiOperation("获取所有文章")
    public Result<PageResult> getArticleList(BackstageArticleQueryDTO backstageArticleQueryDTO) {
        PageResult articleInfoList = backstageArticleService.getAllArticle(backstageArticleQueryDTO);
        return Result.success(articleInfoList);
    }

    @GetMapping("/{id}")
    @ApiOperation("通过id获取文章")
    public Result<ArticleInfoVO> getArticleInfo(@PathVariable Integer id) {
        ArticleInfoVO articleInfoVO = backstageArticleService.getArticleById(id);
        return Result.success(articleInfoVO);
    }
    @GetMapping("/getArticleString/{id}")
    @ApiOperation("通过id获取文章String")
    public Result<String> getArticleByTitle(@PathVariable Integer id) {
        String article = backstageArticleService.getArticleByTitle(id);
        return Result.success(article);
    }

    @GetMapping(value = {"/articleInfoSearch/{title}","/articleInfoSearch"})
    @ApiOperation("通过文章标题，获取id和标题")
    public Result<List<BackstageArticleSearchVO>> getArticleList(@PathVariable(value = "title",required = false) String title) {
        List<BackstageArticleSearchVO> backstageArticleSearchVOS = backstageArticleService.getArticleByContent(title);
        return Result.success(backstageArticleSearchVOS);
    }

    @PutMapping
    @ApiOperation("更改文章内容")
    public Result updateArticle(@RequestBody BackstageArticleUpdateDTO backstageArticleUpdateDTO) {
        backstageArticleService.updateArticle(backstageArticleUpdateDTO);
        // 新增tag
        articleService.saveTag(backstageArticleUpdateDTO.getTags());
        return Result.success("修改成功");
    }

    @PostMapping
    @ApiOperation("新增文章")
    public Result addArticle(@RequestBody BackstageArticleAddDTO backstageArticleAddDTO) {
        backstageArticleService.addArticle(backstageArticleAddDTO);
        // 新增tag
        articleService.saveTag(backstageArticleAddDTO.getTags());
        return Result.success("新增文章成功");
    }

    @DeleteMapping
    @ApiOperation("删除文章")
    public Result deleteArticle(@RequestBody List<Integer> ids) {
        // 通过id删除评论
        backstageCommentService.deleteComment(ids);
        // 删除文章
        backstageArticleService.deleteArticle(ids);
        return Result.success("删除成功");
    }
}
